/**
 * Created by zoushuiyun on 2017/3/28.
 */
import {Injectable} from '@angular/core';
import {Observer, Observable} from 'rxjs';
import {CacheService} from '@core/routes-services/cache.service';
import {BaseService} from '@core/routes-services/base.service';
import {CurrUserModel} from '@core/routes-services/Observer-service/curr-user.model';
import {HttpClient} from '@angular/common/http';

@Injectable()
export class CurrUserService extends BaseService {

  currUserChange: Observable<any>;
  private currUserChangeObserver: Observer<CurrUserModel>;

  constructor(http: HttpClient, cacheService: CacheService) {
    super(http, cacheService);
    this.currUserChange = new Observable<CurrUserModel>((observer) => {
        this.currUserChangeObserver = observer;
      }
    );
  }

  clearCurrUser() {
    const user = new CurrUserModel(null, null, null);
    this.cacheService.setCurrUser(user);
    this.currUserChangeObserver.next(user);
  }

  setCurrUser(currUser: CurrUserModel) {
    this.currUserChangeObserver.next(currUser);
    this.cacheService.setCurrUser(currUser);
  }

}

